import BottomNavigateBar from "@/components/composite/BottomNavigateBar"
import ModalSlideUp from "@/components/composite/ModalSlideUp/main"
import styled from "@emotion/styled"
import { Navigate, useLocation, useMatch, useNavigate } from "react-router-dom"
import SearchActionLayout from "./SearchActionLayout"
import { AnimatedLayoutWithOutletPattern } from "./AnimatedLayout"
import { createLogger } from "@/utils/logger"
import { setNavigate } from "@/utils/navigation"
import { useIsomorphicLayoutEffect } from "ahooks"
import { PATHS } from "@/constants/path"
import kycController from "@/features_signal/kycController"
import { tokenStore } from "@/utils/cached"
import { searchParamsSignal } from "@/apis/api_signals"
const logger = createLogger("ApiFetchers")

const Root = styled.div`
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
`

function RouterRootLayout() {
    const navigate = useNavigate()
    const location = useLocation()
    // 定义不需要显示底部导航的路由
    const hideNavBarPaths = [
        "/login",
        "/loginPassword",
        "/game-page",
        "/my/service",
        "/my/news",
        "/bet-record",
        "/s-game/provider",
        "/s-game/sections",
        "/withdraw-accounts",
        "/s-kyc",
    ]
    const shouldShowNavBar = !hideNavBarPaths.some((path) => location.pathname.startsWith(path))

    useIsomorphicLayoutEffect(() => {
        searchParamsSignal.value = new URLSearchParams(location.search)
        setNavigate(navigate)
    }, [navigate, location.search])

    const token = tokenStore.get()
    const matchKyc = useMatch(PATHS.KYC)
    if (token.accessToken && !matchKyc && kycController.notApprovedWithin72Hours.value) {
        return (
            <Navigate
                to={`${PATHS.KYC}`}
                replace
            />
        )
    }

    return (
        <Root>
            <AnimatedLayoutWithOutletPattern />
            <ModalSlideUp
                familyName="kyc-warning-with-countdown"
                priority={10}
            />
            <ModalSlideUp
                familyName="root"
                priority={0}
            />
            <SearchActionLayout />
            {shouldShowNavBar && <BottomNavigateBar />}
        </Root>
    )
}

export default RouterRootLayout
